package com.mobimagic.lockscreen.view;

import java.util.List;
import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewConfiguration;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.mobimagic.lockscreen.AppEnv;
import com.mobimagic.lockscreen.MobileChargingHelper;
import com.mobimagic.lockscreen.R;
import com.mobimagic.lockscreen.activity.BaseActivity;
import com.mobimagic.lockscreen.activity.MobileChargingActivity;
import com.mobimagic.lockscreen.report.StatKey;
import com.mobimagic.lockscreen.report.Statistician2;
import com.mobimagic.lockscreen.sdk.ILockScreenBooster;
import com.mobimagic.lockscreen.sdk.LockScreenSDK;
import com.mobimagic.lockscreen.util.AppUtils;
import com.mobimagic.lockscreen.util.FastDoubleClickUtils;
import com.mobimagic.lockscreen.util.IntentSkip;
import com.mobimagic.lockscreen.util.SharedPref;

public class FastChargeTipsActivity extends BaseActivity {

    private static final String TAG = "FastChargeTipsActivity";
    private static final boolean DEBUG = AppEnv.bAppdebug;
    private Context mContext;
    private TextView mTitleTextView;
    private TextView mMessageTextView;
    private ImageView mTitleIconImageView;
    private Button mDialogButtons;
    private Handler handler;
    private Runnable runnable;
    private long startTime;
    private long DELAY_THREE_TIME = 4L * 24 * 60 * 60 * 1000;
    private Random random = new Random();
    private int showDialogType;
    public static final int OPEN_FAST_CHARGE_GUIDE_DIALOG = 0;
    public static final int OPEN_FAST_CHARGE_TIPS_DIALOG = 1;
    private ILockScreenBooster mLockScreenBooster;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_game_shortcut);
        mContext = getApplicationContext();
        Intent intent = getIntent();
        if (intent != null) {
            showDialogType = intent.getIntExtra("type", 0);
        }
        initView();
        if (DEBUG) {
            Log.i(TAG, "showDialogType==" + showDialogType);
        }
        if (!isNeedShowForcedDialog()) {
            Statistician2.dbLog(StatKey.STATISTIC_MOBILE_CHARGING_SHOW_GUIDE_DIALOG);
            startTime = System.currentTimeMillis();
            setDisplayDialogTimes();
        } else {
            int appNum;
            mLockScreenBooster = LockScreenSDK.getInstance().getLockScreenBooster();
            mLockScreenBooster.startLoadBoost();
            List<String> boosterListPkg = mLockScreenBooster.getBoosterList();
            if (boosterListPkg != null && boosterListPkg.size() != 0) {
                appNum = boosterListPkg.size();
            } else {
                appNum = random.nextInt(3) + 3;
            }
            String text = String.format(getResources().getString(R.string.the_default_open_msg), appNum);
            mMessageTextView.setText(text);
            Statistician2.dbLog(StatKey.STATISTIC_MOBILE_CHARGING_SHOW_DEFAULT_OPEN_DIALOG);
        }
    }

    public boolean isNeedShowForcedDialog() {
        return showDialogType == OPEN_FAST_CHARGE_TIPS_DIALOG;
    }

    private void setDisplayDialogTimes() {
        handler = new Handler();
        runnable = new Runnable() {

            @Override
            public void run() {
                int ClickTimes = SharedPref.getInt(mContext, SharedPref.FAST_CHARGE_GUIDE_DIALOG_DISPLAY_TIMES, 0) + 1;
                if (DEBUG) {
                    Log.i(TAG, "ClickTimes==" + ClickTimes);
                }
                SharedPref.setInt(mContext, SharedPref.FAST_CHARGE_GUIDE_DIALOG_DISPLAY_TIMES, ClickTimes);
                SharedPref.setLong(mContext, SharedPref.FAST_CHARGE_GUIDE_DIALOG_LAST_DISPLAY_TIME,
                        System.currentTimeMillis());
            }
        };
        handler.postDelayed(runnable, 2000);
    }

    private void initView() {
        View containerView = findViewById(R.id.dialog_container);
        mTitleTextView = (TextView) findViewById(R.id.dialog_title);
        mMessageTextView = (TextView) findViewById(R.id.dialog_summery);
        mTitleIconImageView = (ImageView) findViewById(R.id.dialog_icon);
        mDialogButtons = (Button) findViewById(R.id.dialog_button);

        LayoutParams params = containerView.getLayoutParams();
        params.width = AppUtils.getWidth(mContext) - AppUtils.dip2px(mContext, 60);
        containerView.setLayoutParams(params);

        if (!isNeedShowForcedDialog()) {
            mTitleTextView.setText(R.string.enable_speed_charge_title);
            mMessageTextView.setText(R.string.fast_charge_guide_dialog_msg);
            mTitleIconImageView.setImageResource(R.drawable.fast_charge_tip_icon);
            mTitleIconImageView.setVisibility(View.VISIBLE);
            mDialogButtons.setText(R.string.enable);
        } else {
            mTitleTextView.setText(R.string.boostcharge);
            mDialogButtons.setText(R.string.booster_speed_up);
        }
        mDialogButtons.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (FastDoubleClickUtils.isFastDoubleClick()) {
                    return;
                }
                if (!isNeedShowForcedDialog()) {
                    MobileChargingHelper.getInstance().setOpenFastChargeFunc(true);
                    Statistician2.dbLog(StatKey.STATISTIC_MOBILE_CHARGING_CLICK_GUILD_DIALOG_BUTTON);
                } else {
                    Statistician2.dbLog(StatKey.STATISTIC_MOBILE_CHARGING_CLICK_DEFAULT_OPEN_DIALOG);
                }
                IntentSkip.skipToBatteryCharging(mContext, MobileChargingActivity.EXTRA_FROM_CLICK_TRIP_BTN);
                finish();
            }
        });
    }

    private boolean isOutOfBounds(Activity context, MotionEvent event) {
        final int x = (int) event.getX();
        final int y = (int) event.getY();
        final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
        final View decorView = context.getWindow().getDecorView();
        return (x < -slop) || (y < -slop) || (x > (decorView.getWidth() + slop))
                || (y > (decorView.getHeight() + slop));
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN && isOutOfBounds(this, event)) {
            finish();
            return true;
        }
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (!isNeedShowForcedDialog()) {
            handler.removeCallbacks(runnable);
            long dTime = System.currentTimeMillis() - startTime;
            if (dTime < 2000) {//如果弹窗消失在2秒之内，3天后再显示
                long currentTime = System.currentTimeMillis() - DELAY_THREE_TIME;
                if (DEBUG) {
                    Log.i(TAG,
                            "Meet the conditions, three days later return to the main interface is displayed. currentTime=="
                                    + currentTime);
                }
                SharedPref.setLong(mContext, SharedPref.FAST_CHARGE_GUIDE_DIALOG_LAST_DISPLAY_TIME, currentTime);
            }
        }
    }
}
